package com.zz.Stream;

import java.util.*;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Stream;

public class Test {

    private static List<Author> getAuthors() {
        //数据初始化
        Author author = new Author(1L,"蒙多",33,"一个从菜刀中明悟哲理的祖安人",null);
        Author author2 = new Author(2L,"亚拉索",15,"狂风也追逐不上他的思考速度",null);
        Author author3 = new Author(3L,"易",14,"是这个世界在限制他的思维",null);
        Author author4 = new Author(3L,"易",14,"是这个世界在限制他的思维",null);

        //书籍列表
        List<Book> books1 = new ArrayList<>();
        List<Book> books2 = new ArrayList<>();
        List<Book> books3 = new ArrayList<>();

        books1.add(new Book(1L,"刀的两侧是光明与黑暗","哲学,爱情",88,"用一把刀划分了爱恨"));
        books1.add(new Book(2L,"一个人不能死在同一把刀下","个人成长,爱情",99,"讲述如何从失败中明悟真理"));

        books2.add(new Book(3L,"那风吹不到的地方","哲学",85,"带你用思维去领略世界的尽头"));
        books2.add(new Book(3L,"那风吹不到的地方","哲学",85,"带你用思维去领略世界的尽头"));
        books2.add(new Book(4L,"吹或不吹","爱情,个人传记",56,"一个哲学家的恋爱观注定很难把他所在的时代理解"));

        books3.add(new Book(5L,"你的剑就是我的剑","爱情",56,"无法想象一个武者能对他的伴侣这么的宽容"));
        books3.add(new Book(6L,"风与剑","个人传记",100,"两个哲学家灵魂和肉体的碰撞会激起怎么样的火花呢？"));
        books3.add(new Book(6L,"风与剑","个人传记",100,"两个哲学家灵魂和肉体的碰撞会激起怎么样的火花呢？"));

        author.setBooks(books1);
        author2.setBooks(books2);
        author3.setBooks(books3);
        author4.setBooks(books3);

        List<Author> authorList = new ArrayList<>(Arrays.asList(author,author2,author3,author4));
        return authorList;
    }

    public static void main(String[] args) {
        test08();
    }

    //创建流,有两种方式
    public static void test01(){
        Integer[] list={1,2,3,4,5};
        Stream<Integer> stream=Arrays.stream(list);
        Stream<Integer> stream1 = Stream.of(list);
        stream.distinct().filter(integer -> integer<3).forEach(integer -> System.out.println(integer));
        stream1.distinct().filter(integer -> integer<3).forEach(integer -> System.out.println(integer));
    }

    public static void test02(){
        Map<String,Integer> map = new HashMap<>();
        map.put("蜡笔小新",19);
        map.put("黑子",17);
        map.put("日向翔阳",16);

        Stream<Map.Entry<String, Integer>> stream = map.entrySet().stream();

        stream.filter(new Predicate<Map.Entry<String, Integer>>() {
            @Override
            public boolean test(Map.Entry<String, Integer> stringIntegerEntry) {
                return stringIntegerEntry.getValue()>16;
            }
        }).forEach(stringIntegerEntry -> System.out.println(stringIntegerEntry));
    }

    public static void test03(){
        //输出作者的描述
        getAuthors()
                .stream()
                .map(Author::getIntro)
                .forEach(author-> System.out.println(author));
    }

    public static void test04(){
        //按年龄升序的顺序排列
        getAuthors()
                .stream()
                .distinct()
                .sorted(new Comparator<Author>() {
                    @Override
                    public int compare(Author o1, Author o2) {
                        return o1.getAge()- o2.getAge();
                    }
                })
                .forEach(author -> System.out.println(author));
    }

    public static void test05(){
        getAuthors()
                .stream()
                .distinct()
                .sorted()
                .skip(1)
                .forEach(author -> System.out.println(author));
    }

    public static void test06(){
        List<Author> authors = getAuthors();
        authors.stream()
                .flatMap(new Function<Author, Stream<Book>>() {
                    @Override
                    public Stream<Book> apply(Author author) {
                        System.out.println(author.getBooks());
                        return  author.getBooks().stream();
                    }
                })
                .distinct()
                .flatMap(book -> Arrays.stream(book.getCategory().split(",")))
                .distinct()
                .forEach(category-> System.out.println(category));
    }

    public static void test07(){
        // 输出所有作家的名字
        getAuthors().stream()
                .map(ao->ao.getName())
                .distinct()
                .forEach(name-> System.out.println(name));
    }

    public static void test08(){
        //        打印这些作家的所出书籍的数目，注意删除重复元素。
        long count=getAuthors().stream()
                .flatMap(author -> author.getBooks().stream())
                .distinct()
                .count();
        System.out.println(count);
    }

}
